package com.sinsoft.monitorDemo.dao;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import redis.clients.jedis.Jedis;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.sinsoft.monitorDemo.entity.MysqlInfo;
import com.sinsoft.monitorDemo.util.JsonUtil;
import com.sinsoft.monitorDemo.util.RedisUtil;

public class MysqlInfoDao {
	
	public List<MysqlInfo> getMysqlInfoList(String ip){
		Jedis jedis = RedisUtil.getJedis();
		List<String> jsonList = jedis.lrange("StatusData_" + ip + "_MYSQL_latest", 0, -1);
		JsonArray jsonArray = null;
		JsonObject jsonObject = null;
		MysqlInfo mysqlInfo = null;
		List<MysqlInfo> mysqlInfoList = new ArrayList<MysqlInfo>();
		for (int i = jsonList.size()-10; i < jsonList.size(); i++) {
			Map<Integer,String> map = new LinkedHashMap<Integer,String>();	
			jsonArray = JsonUtil.JsonParse(jsonList.get(i));
			jsonObject = jsonArray.get(0).getAsJsonObject();
			JsonArray array = jsonObject.get("port_status").getAsJsonArray();
			for (int j = 0; j < array.size(); j++) {
				JsonArray array2 = array.get(j).getAsJsonArray();
				int key = array2.get(0).getAsInt();
				String value = array2.get(1).getAsString();
				map.put(key, value);
			}
			mysqlInfo = new MysqlInfo(jsonObject.get("cpu_used_per").getAsDouble(), 
					jsonObject.get("mem_used_per").getAsDouble(), map, jsonObject.get("status").getAsString(), 
					jsonObject.get("process_current").getAsInt(), jsonObject.get("connections").getAsInt(), 
					jsonObject.get("connections_active").getAsInt(), jsonObject.get("max_connections").getAsInt(), 
					jsonObject.get("slow_queries").getAsInt(),jsonObject.get("long_query_time").getAsDouble(),jsonArray.get(1).getAsLong());			
			mysqlInfoList.add(mysqlInfo);
		}
		return mysqlInfoList;		
	}
	
	/**
	 * 得到最新的disk信息
	 * @param ip
	 * @return
	 */
	public MysqlInfo getNewMysqlInfo(String ip){
		Jedis jedis = RedisUtil.getJedis();
		Map<Integer,String> map = new LinkedHashMap<Integer,String>();	
		MysqlInfo mysqlInfo;
		try {
			List<String> jsonList = jedis.lrange("StatusData_" + ip + "_MYSQL_latest", -1, -1);
			JsonArray jsonArray = JsonUtil.JsonParse(jsonList.get(0));
			JsonObject jsonObject = jsonArray.get(0).getAsJsonObject();
			JsonArray array = jsonObject.get("port_status").getAsJsonArray();
			for (int j = 0; j < array.size(); j++) {
				JsonArray array2 = array.get(j).getAsJsonArray();
				int key = array2.get(0).getAsInt();
				String value = array2.get(1).getAsString();
				map.put(key, value);
			}
			mysqlInfo = new MysqlInfo(jsonObject.get("cpu_used_per").getAsDouble(), 
					jsonObject.get("mem_used_per").getAsDouble(), map, 
					jsonObject.get("status").getAsString(), jsonObject.get("process_current").getAsInt(), 
					jsonObject.get("connections").getAsInt(), jsonObject.get("connections_active").getAsInt(), 
					jsonObject.get("max_connections").getAsInt(), jsonObject.get("slow_queries").getAsInt(),
					jsonObject.get("long_query_time").getAsDouble(),jsonArray.get(1).getAsLong());
		} catch (Exception e) {
			return new MysqlInfo(null, null, null, null, null, null, null, null, null, null, null);
		}finally{
			jedis.close();
		}			
		return mysqlInfo;		
	}
}
